package itheima_5;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadSocket implements Runnable{
    private Socket as;
    public ThreadSocket(Socket accept) {
        this.as=accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos=null;
        try {

            BufferedInputStream bis=new BufferedInputStream(as.getInputStream());
           bos =new BufferedOutputStream(new FileOutputStream("Inte\\ser\\"+UUID.randomUUID().toString()+".jpg"));
            int b;
            while ((b=bis.read())!=-1){
                bos.write(b);
            }
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(as.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos!=null){

                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(as!=null){

                try {
                    as.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
